package com.ktg.mes.dv.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ktg.common.core.domain.BaseEntity;
import com.ktg.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.SecurityUtils;
import com.ktg.mes.dv.domain.DvWattHourMeterData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktg.mes.dv.mapper.DvWarmHumidDataMapper;
import com.ktg.mes.dv.domain.DvWarmHumidData;
import com.ktg.mes.dv.service.IDvWarmHumidDataService;



/**
 * 温湿数据Service业务层处理
 *
 * @author byteco
 * @date 2022-11-30
 */
@Service
public class DvWarmHumidDataServiceImpl extends ServiceImpl<DvWarmHumidDataMapper, DvWarmHumidData> implements IDvWarmHumidDataService
{




    /**
     * 查询温湿数据
     *
     * @param entity 温湿数据
     * @param page 分页对象
     * @return 温湿数据
     */
    @Override
    public IPage<DvWarmHumidData> queryPageList(DvWarmHumidData entity, Page<DvWarmHumidData> page) {
        // QueryWrapper<DvWarmHumidData> lqw = new QueryWrapper<DvWarmHumidData>(entity);
        // lqw.orderByDesc("create_time");
        LambdaQueryWrapper<DvWarmHumidData> lqw = buildQueryWrapper(entity);
        lqw.orderByDesc(BaseEntity::getCreateTime);
        IPage<DvWarmHumidData> pageList = this.page(page, lqw);
        return pageList;
    }

    /**
     * 查询温湿数据
     *
     * @param entity 温湿数据
     * @return 温湿数据
     */
    @Override
    public List<DvWarmHumidData> queryList(DvWarmHumidData entity) {
        // QueryWrapper<DvWarmHumidData> lqw = new QueryWrapper<DvWarmHumidData>(entity);
        // lqw.orderByDesc("create_time");
        LambdaQueryWrapper<DvWarmHumidData> lqw = buildQueryWrapper(entity);
        lqw.orderByDesc(BaseEntity::getCreateTime);
        return this.list(lqw);
    }

    public DvWarmHumidData getInfo(Long id)
    {
        return this.getById(id);
    }


    /**
     * 新增温湿数据
     *
     * @param entity 温湿数据
     * @return 结果
     */
    @Override
    public void insert(DvWarmHumidData entity)
    {
        entity.setCreateTime(DateUtils.getNowDate());
        this.save(entity);
    }

    /**
     * 修改温湿数据
     *
     * @param entity 温湿数据
     * @return 结果
     */
    @Override
    public boolean update(DvWarmHumidData entity)
    {
        entity.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(entity);
    }

    /**
     * 批量删除温湿数据
     *
     * @param recordIds 需要删除的温湿数据主键
     * @return 结果
     */
    @Override
    public boolean delete(Long[] recordIds)
    {

        return this.removeByIds(Arrays.asList(recordIds));
    }

    /**
     * 删除温湿数据信息
     *
     * @param recordId 温湿数据主键
     * @return 结果
     */
    @Override
    public boolean delete(Long recordId)
    {
        return this.removeById(recordId);
    }

    public List<Map>chartMinute(Wrapper ew, String selectColumn){
        return baseMapper.chartMinute(ew,selectColumn);
    }
    /**
     * 构建查询条件
     * @param entity
     * @return
     */
    private LambdaQueryWrapper<DvWarmHumidData> buildQueryWrapper(DvWarmHumidData entity){
        // QueryWrapper<DvWattHourMeterData> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<DvWarmHumidData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(entity.getBatchNo()),DvWarmHumidData::getBatchNo,entity.getBatchNo());
        queryWrapper.eq(StringUtils.isNotBlank(entity.getDeviceCode()),DvWarmHumidData::getDeviceCode,entity.getDeviceCode());
        queryWrapper.eq(StringUtils.isNotBlank(entity.getProcessName()),DvWarmHumidData::getProcessName,entity.getProcessName());
        queryWrapper.apply(ObjectUtils.isNotNull(entity.getCreateTime()),"DATE_FORMAT(create_time,'%Y-%m-%d') = DATE_FORMAT({0},'%Y-%m-%d')",entity.getCreateTime());
        return queryWrapper;
    }

}
